package com.hj2022java.autotest;

import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeDriver;

import java.time.Duration;

/**
 * \* Created with IntelliJ IDEA.
 * \* User: Administrator
 * \* Date: 2022/11/5
 * \* Time: 15:09
 * \* To change this template use File | Settings | File Templates.
 * \* Description:
 * \
 */
public class firstautotest {
    ChromeDriver driver = new ChromeDriver();
    public void start_test() throws InterruptedException {
        driver.get("https://www.baidu.com");
        Thread.sleep(2000);
    }

    //一个简单的自动化示例
    public void dilireba_test() throws InterruptedException {
        ChromeDriver driver = new ChromeDriver();
        Thread.sleep(2000);
        //输入百度网址
        driver.get("https://www.baidu.com");
        Thread.sleep(2000);
        //找到百度输入框，并输入关键字
        driver.findElement(By.cssSelector("#kw")).sendKeys("努力学习");
        Thread.sleep(2000);
        //找到百度一下按钮，并点击
        driver.findElement(By.cssSelector("#su")).click();
        Thread.sleep(2000);
        driver.findElement(By.id("kw")).sendKeys("天天向上");
        //释放掉驱动对象，关闭浏览器
        driver.quit();
    }

    /**
     * 定位元素的方法
     */
    public void dingwei_Test() throws InterruptedException {
        //css选择器的定位
//        String xwtest = driver.findElement(By.cssSelector("#s-top-left > a:nth-child(1)")).getText();
//        System.out.println("xatest:"+xwtest);
        //xpath选择器
//        driver.findElement(By.xpath("//*[@id=\"kw\"]")).sendKeys("selenium");
//        driver.findElement(By.xpath("//*[@id=\"su\"]")).click();
//        Thread.sleep(3000);
//        driver.findElement(By.xpath("//*[@id=\"2\"]/div/div/h3/a")).click();
//        Thread.sleep(3000);

//          String xwtxt = driver.findElement(By.xpath("//*[@id=\"hotsearch-content-wrapper\"]/li[3]/a/span[2]")).getText();
//        System.out.println("期望的文本是："+xwtxt);

    }

    /**
     * 元素的操作
     * @throws InterruptedException
     */

    public void control_Test() throws InterruptedException {
//        driver.findElement(By.xpath("//*[@id=\\\"su\\\"]")).click();
//        Thread.sleep(3000);
        //sumbit提交行为
//        driver.findElement(By.cssSelector("#kw")).sendKeys("selenium");
//        Thread.sleep(3000);
//        driver.findElement(By.cssSelector("#su")).submit();
//        Thread.sleep(3000);
//        driver.findElement(By.cssSelector("#kw")).sendKeys("新东方");
//        Thread.sleep(3000);
//        driver.findElement(By.cssSelector("#kw")).clear();
//        Thread.sleep(3000);
//        driver.findElement(By.cssSelector("#kw")).sendKeys("周杰伦");
//        Thread.sleep(3000);

        //如何获取属性值
        String bdtext = driver.findElement(By.cssSelector("#su")).getAttribute("value");
        System.out.println(bdtext);
        //隐式等待
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(3));
        //显示等待

    }
    public void location_Text() throws InterruptedException {
        //css选择器
        //通过css选择器定位到输入框，并且输入堂吉诃德
//        driver.findElement(By.cssSelector("#kw")).sendKeys("堂吉诃德");
//        //等待3s
//        Thread.sleep(3000);
//        //通过css选择器定位到百度一下按钮，并且进行点击操作
//        driver.findElement(By.cssSelector("#su")).click();
//        Thread.sleep(3000);


//        //xpath选择器定位
//        driver.findElement(By.xpath("//*[@id=\"kw\"]")).sendKeys("堂吉诃德");
//        Thread.sleep(3000);
//        driver.findElement(By.xpath("//*[@id=\"su\"]")).click();
//        Thread.sleep(3000);

        //id定位
        driver.findElement(By.id("kw")).sendKeys("堂吉诃德");
        Thread.sleep(3000);
        String text = driver.findElement(By.cssSelector("#kw")).getText();
        System.out.println(text);
        driver.findElement(By.id("su")).click();
        Thread.sleep(3000);
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(3));

    }

    public void end_Test() throws InterruptedException {
        Thread.sleep(2000);
        //释放掉驱动对象，关闭浏览器
        driver.quit();
    }
}
